<template>
  <div class="tab-bar">
    <van-tabbar v-model="curIndex" active-color="var(--primary-color)" route>
      <van-tabbar-item v-for="(item, index) in tabbarData" :key="item.text" :to="item.path">
        <!-- 匿名插槽 -->
        <span>{{ item.text }}</span>
        <!-- 具名插槽icon -->
        <template #icon>
          <img v-if="index === curIndex" :src="getAssetImg(item.imgActive)" alt="">
          <img v-else :src="getAssetImg(item.img)" alt="">
        </template>
      </van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { getAssetImg } from '../../utils/getAssetImg.js'

const tabbarData = [
  {
    text: '首页',
    img: 'tabbar/tab_home.png',
    imgActive: 'tabbar/tab_home_active.png',
    path: '/home'
  },
  {
    text: '收藏',
    img: 'tabbar/tab_favor.png',
    imgActive: 'tabbar/tab_favor_active.png',
    path: '/favor'
  },
  {
    text: '订单',
    img: 'tabbar/tab_order.png',
    imgActive: 'tabbar/tab_order_active.png',
    path: '/order'
  },
  {
    text: '消息',
    img: 'tabbar/tab_message.png',
    imgActive: 'tabbar/tab_message.png',
    path: '/message'
  },
]

// 当前下标
let curIndex = ref(0)

</script>

<style scoped lang="less">
.tab-bar {
  // 避免底部遮挡
  height: 50px;
  :deep(.van-tabbar-item__text) {
    font-size: 14px;
  }
}
</style>
